<%--
  Created by IntelliJ IDEA.
  User: zhoufei
  Date: 2025/5/25
  Time: 10:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册页面</title>
    <style>
        /* 沿用管理系统样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', sans-serif;
        }

        body {
            background-color: #f0f2f5;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .register-container {
            background: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 8px 24px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 440px;
            margin: 20px;
        }

        .register-header {
            text-align: center;
            margin-bottom: 32px;
        }

        .logo {
            color: #001529;
            font-size: 28px;
            font-weight: bold;
            margin-bottom: 8px;
        }

        .form-group {
            margin-bottom: 24px;
            position: relative;
        }

        .form-label {
            display: block;
            color: #666;
            margin-bottom: 8px;
            font-size: 14px;
            font-weight: 500;
        }

        .form-input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e8e8e8;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s;
        }

        .form-input:focus {
            border-color: #1890ff;
            box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.15);
            outline: none;
        }

        .error-message {
            color: #ff4d4f;
            font-size: 12px;
            margin-top: 6px;
            display: none;
        }

        .submit-btn {
            width: 100%;
            padding: 14px;
            background: #1890ff;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: 0.3s;
        }

        .submit-btn:hover {
            background: #1475d9;
        }

        .login-link {
            text-align: center;
            margin-top: 24px;
            color: #666;
        }

        .login-link a {
            color: #1890ff;
            text-decoration: none;
        }

        /* 验证码样式 */
        .captcha-group {
            display: flex;
            gap: 12px;
        }

        .captcha-input {
            flex: 1;
        }

        .captcha-img {
            height: 46px;
            border-radius: 6px;
            cursor: pointer;
            border: 1px solid #e8e8e8;
        }

        /* 加载状态 */
        .submit-btn.loading {
            pointer-events: none;
            opacity: 0.8;
            position: relative;
            padding-right: 40px;
        }

        .submit-btn.loading::after {
            content: "";
            position: absolute;
            right: 15px;
            top: 50%;
            width: 20px;
            height: 20px;
            border: 2px solid rgba(255,255,255,0.3);
            border-top-color: white;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            transform: translateY(-50%);
        }

        @keyframes spin {
            to { transform: translateY(-50%) rotate(360deg); }
        }
    </style>
</head>
<body>
<div class="register-container">
    <div class="register-header">
        <div class="logo">XXX管理系统</div>
        <h2>用户注册</h2>
    </div>

    <form id="registerForm">
        <!-- 用户名 -->
        <div class="form-group">
            <label class="form-label">用户名</label>
            <input type="text" class="form-input" name="username"
                   placeholder="4-16位字母/数字/下划线" required>
            <div class="error-message" id="usernameError"></div>
        </div>

        <!-- 密码 -->
        <div class="form-group">
            <label class="form-label">密码</label>
            <input type="password" class="form-input" name="password"
                   placeholder="至少8位，包含字母和数字" required>
            <div class="error-message" id="passwordError"></div>
        </div>

        <!-- 确认密码 -->
        <div class="form-group">
            <label class="form-label">确认密码</label>
            <input type="password" class="form-input" name="confirmPassword" required>
            <div class="error-message" id="confirmError"></div>
        </div>

        <!-- 邮箱 -->
        <div class="form-group">
            <label class="form-label">电子邮箱</label>
            <input type="email" class="form-input" name="email"
                   placeholder="example@domain.com" required>
            <div class="error-message" id="emailError"></div>
        </div>

        <!-- 验证码 -->
        <div class="form-group">
            <div class="captcha-group">
                <input type="text" class="form-input captcha-input"
                       placeholder="输入验证码" required>
                <img src="captcha-placeholder.jpg" class="captcha-img"
                     alt="验证码" title="点击刷新" onclick="refreshCaptcha()">
            </div>
            <div class="error-message" id="captchaError"></div>
        </div>

        <!-- 协议条款 -->
        <div class="form-group">
            <label style="display: flex; align-items: center; gap: 8px;">
                <input type="checkbox" required>
                我已阅读并同意《服务条款》
            </label>
        </div>

        <button type="submit" class="submit-btn">立即注册</button>
    </form>

    <div class="login-link">
        已有账号？ <a href="login.html">立即登录</a>
    </div>
</div>
<script>
    // 表单验证逻辑
    const form = document.getElementById('registerForm');
    const errorFields = {
        username: document.getElementById('usernameError'),
        password: document.getElementById('passwordError'),
        confirm: document.getElementById('confirmError'),
        email: document.getElementById('emailError'),
        captcha: document.getElementById('captchaError')
    };

    // 用户名验证
    function validateUsername(username) {
        const regex = /^[a-zA-Z0-9_]{4,16}$/;
        if (!regex.test(username)) {
            return '用户名需4-16位字母/数字/下划线';
        }
        return '';
    }

    // 密码验证
    function validatePassword(password) {
        const regex = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/;
        if (!regex.test(password)) {
            return '密码需至少8位，包含字母和数字';
        }
        return '';
    }

    // 确认密码验证
    function validateConfirm(password, confirm) {
        return password === confirm ? '' : '两次输入的密码不一致';
    }

    // 邮箱验证
    function validateEmail(email) {
        const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        return regex.test(email) ? '' : '请输入有效的邮箱地址';
    }

    // 提交处理
    form.addEventListener('submit', async (e) => {
        e.preventDefault();
        const formData = new FormData(form);
        const data = Object.fromEntries(formData.entries());

        // 清除旧错误
        Object.values(errorFields).forEach(el => el.style.display = '');

        // 验证字段
        let isValid = true;

        // 用户名验证
        const usernameError = validateUsername(data.username);
        if (usernameError) {
            errorFields.username.textContent = usernameError;
            errorFields.username.style.display = 'block';
            isValid = false;
        }

        // 密码验证
        const passwordError = validatePassword(data.password);
        if (passwordError) {
            errorFields.password.textContent = passwordError;
            errorFields.password.style.display = 'block';
            isValid = false;
        }

        // 确认密码
        const confirmError = validateConfirm(data.password, data.confirmPassword);
        if (confirmError) {
            errorFields.confirm.textContent = confirmError;
            errorFields.confirm.style.display = 'block';
            isValid = false;
        }

        // 邮箱验证
        const emailError = validateEmail(data.email);
        if (emailError) {
            errorFields.email.textContent = emailError;
            errorFields.email.style.display = 'block';
            isValid = false;
        }

        if (!isValid) return;

        // 模拟提交
        const submitBtn = form.querySelector('button[type="submit"]');
        submitBtn.classList.add('loading');

        // 模拟API请求
        setTimeout(() => {
            submitBtn.classList.remove('loading');
            // 这里替换为真实API调用
            if (Math.random() > 0.2) { // 模拟80%成功率
                alert('注册成功！即将跳转到登录页面');
                window.location.href = 'login.html';
            } else {
                errorFields.captcha.textContent = '验证码错误';
                errorFields.captcha.style.display = 'block';
                refreshCaptcha();
            }
        }, 1500);
    });

    // 验证码刷新
    function refreshCaptcha() {
        const captchaImg = document.querySelector('.captcha-img');
        captchaImg.src = 'captcha-placeholder.jpg?t=' + Date.now();
    }

    // 实时验证
    form.addEventListener('input', (e) => {
        const field = e.target.name;
        const value = e.target.value;

        switch(field) {
            case 'username':
                errorFields.username.style.display = validateUsername(value) ? 'block' : 'none';
                break;
            case 'password':
                errorFields.password.style.display = validatePassword(value) ? 'block' : 'none';
                errorFields.confirm.style.display = validateConfirm(
                    value,
                    form.confirmPassword.value
                ) ? 'block' : 'none';
                break;
            case 'confirmPassword':
                errorFields.confirm.style.display = validateConfirm(
                    form.password.value,
                    value
                ) ? 'block' : 'none';
                break;
            case 'email':
                errorFields.email.style.display = validateEmail(value) ? 'block' : 'none';
                break;
        }
    });
</script>
</body>
</html>
